import pytest
from common.request_util_test import RequestUtil
from common.test_yaml_util_test import YamlUtil
import json

class Testrolebinding:

    @pytest.mark.parametrize('rolebindinginfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/rolebinding/1_get_rolebinding_list.yaml'))
    def test_get_rolebinding_list(self, rolebindinginfo, get_env_info):
        # 获取rolebinding列表接口
        baseUrl = get_env_info
        url = baseUrl + rolebindinginfo['request']['url'].replace('{namespace}','default')
        method = rolebindinginfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == rolebindinginfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('rolebindinginfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/rolebinding/2_create_rolebinding.yaml'))
    def test_create_rolebinding(self, rolebindinginfo, get_env_info):
        # 创建rolebinding接口
        baseUrl = get_env_info
        url = baseUrl + rolebindinginfo['request']['url'].replace('{namespace}','default')
        method = rolebindinginfo['request']['method']
        data = rolebindinginfo['request']['data']
        req = RequestUtil().send_request(method, url,json=data)
        if '-' not in rolebindinginfo['name']:
            rolebindingname=req.json()['metadata']['name']
            globals()['rolebindingname']=rolebindingname
        assert req.status_code == rolebindinginfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('rolebindinginfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/rolebinding/3_get_specific_rolebinding.yaml'))
    def test_get_specific_rolebinding(self, rolebindinginfo, get_env_info):
        # 获取指定rolebinding接口
        baseUrl = get_env_info
        url = baseUrl + rolebindinginfo['request']['url'].replace('{namespace}','default').replace('{rolebindingname}',globals()['rolebindingname'])
        method = rolebindinginfo['request']['method']
        req = RequestUtil().send_request(method, url)
        if '-' not in rolebindinginfo['name']:
            updatereqbody=req.json()
            globals()['updatereqbody']=updatereqbody
        assert req.status_code == rolebindinginfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('rolebindinginfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/rolebinding/4_update_rolebinding.yaml'))
    def test_update_rolebinding(self, rolebindinginfo, get_env_info):
        # 更新指定rolebinding接口
        baseUrl = get_env_info
        url = baseUrl + rolebindinginfo['request']['url'].replace('{namespace}','default').replace('{rolebindingname}',globals()['rolebindingname'])
        method = rolebindinginfo['request']['method']
        rolebindinginfo['request']['data'] = globals()['updatereqbody']
        data = rolebindinginfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == rolebindinginfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('rolebindinginfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/rolebinding/5_update_section_rolebinding.yaml'))
    def test_update_section_rolebinding(self, rolebindinginfo, get_env_info):
        # 部分更新指定rolebinding的接口
        baseUrl = get_env_info
        url = baseUrl + rolebindinginfo['request']['url'].replace('{namespace}','default').replace('{rolebindingname}',globals()['rolebindingname'])
        method = rolebindinginfo['request']['method']
        data = rolebindinginfo['request']['data']
        header = rolebindinginfo['request']['headers']
        req = RequestUtil().send_request(method, url, headers=header,json=data)
        assert req.status_code == rolebindinginfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('rolebindinginfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/rolebinding/6_delete_specific_rolebinding.yaml'))
    def test_delete_specific_rolebinding(self, rolebindinginfo, get_env_info):
        # 删除rolebinding接口
        baseUrl = get_env_info
        url = baseUrl + rolebindinginfo['request']['url'].replace('{namespace}','default').replace('{rolebindingname}',globals()['rolebindingname'])
        method = rolebindinginfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == rolebindinginfo['validate'][0]['equals']['status_code']